package com.monkey.netty.chapter5;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelOutboundHandlerAdapter;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class ExceptionDemo {

	public static void main(String[] args) {
		ServerBootstrap serverBootstrap = new ServerBootstrap();
		NioEventLoopGroup bossGroup = new NioEventLoopGroup();
		NioEventLoopGroup workerGroup = new NioEventLoopGroup();
		serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
				.option(ChannelOption.SO_BACKLOG, 1024).childOption(ChannelOption.TCP_NODELAY, true)
				.childHandler(new ChannelInitializer<Channel>() {
					@Override
					protected void initChannel(Channel channel) throws Exception {
						/**
						 * 依次添加A、B、C三个inbound处理器和outbound处理器
						 */
						channel.pipeline().addLast(new ExceptionInboundHandlerA());
						channel.pipeline().addLast(new ExceptionInboundHandlerB());
						channel.pipeline().addLast(new ExceptionInboundHandlerC());
						channel.pipeline().addLast(new ExceptionOutboundHandlerA());
						channel.pipeline().addLast(new ExceptionOutboundHandlerB());
						channel.pipeline().addLast(new ExceptionOutboundHandlerC());
						channel.pipeline().addLast(new ExceptionHandler());
					}
				});
		try {
			ChannelFuture channelFuture = serverBootstrap.bind(8888).sync();
			channelFuture.channel().closeFuture().sync();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			bossGroup.shutdownGracefully();
			workerGroup.shutdownGracefully();
		}
	}
}

class ExceptionInboundHandlerA extends ChannelInboundHandlerAdapter{

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		System.out.println("ExceptionInboundHandlerA");
		ctx.fireExceptionCaught(cause);
	}
	
}

class ExceptionInboundHandlerB extends ChannelInboundHandlerAdapter{

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		throw new RuntimeException();
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		System.out.println("ExceptionInboundHandlerB");
		ctx.fireExceptionCaught(cause);
	}
	
}

class ExceptionInboundHandlerC extends ChannelInboundHandlerAdapter{

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		System.out.println("ExceptionInboundHandlerC");
		ctx.fireExceptionCaught(cause);
	}
	
}

class ExceptionOutboundHandlerA extends ChannelOutboundHandlerAdapter{

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		System.out.println("ExceptionOutboundHandlerA");
		ctx.fireExceptionCaught(cause);
	}
	
}
class ExceptionOutboundHandlerB extends ChannelOutboundHandlerAdapter{

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		System.out.println("ExceptionOutboundHandlerB");
		ctx.fireExceptionCaught(cause);
	}
	
}
class ExceptionOutboundHandlerC extends ChannelOutboundHandlerAdapter{

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		System.out.println("ExceptionOutboundHandlerC");
		ctx.fireExceptionCaught(cause);
	}
	
}

class ExceptionHandler extends ChannelInboundHandlerAdapter {

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		if (cause instanceof RuntimeException) {
			System.out.println("捕获到一个异常,进行异常处理.");
		}
	}
	
}